<template>
  <div class="login-container">
    <el-row :gutter="20">
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
                 class="login_form"
                 :model="loginForm"
                 :rules="rules"
                 ref="loginForms">
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input
                      :prefix-icon="User"
                      v-model="loginForm.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
                      type="password"
                      :prefix-icon="Lock"
                      v-model="loginForm.password"
                      show-password></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
                       :loading="loading"
                       type="primary"
                       class="login_btn"
                       @click="login">登录</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from "@element-plus/icons-vue";
import { reactive, ref } from "vue";
import { ElNotification } from "element-plus";
// 引入获取当前时间的函数
import { getTime } from "@/utils/time";
// 导入用户相关的二级仓库
import useUserStore from "@/store/modules/user";
// 导入 useRouter 方法
import { useRouter, useRoute } from "vue-router";
// 获取路由器
const $router = useRouter();
// 获取路由
const $route = useRoute();
// 获取仓库
const userStore = useUserStore();
// 获取 el-form 组件
const loginForms = ref();
// 定义变量控制按钮加载效果
const loading = ref(false);
// 收集表单数据
const loginForm = reactive({ username: "admin", password: "111111" });
// 登录操作
const login = async () => {
  // 保证全部的表单项校验通过再发请求
  await loginForms.value.validate();
  // 开启加载效果
  loading.value = true;
  try {
    // 保证登录成功
    await userStore.userLogin(loginForm);
    // 编程式导航跳转到展示数据的首页
    // 判断登录时路径是否有 query 参数
    const redirect: any = $route.query.redirect;
    $router.push({ path: redirect || "/" });
    // 登陆成功的提示信息
    ElNotification({
      type: "success",
      message: "欢迎回来",
      title: `Hi,${getTime()}好`,
    });
    // 登录成功关闭加载效果
    loading.value = false;
  } catch (error) {
    // 登录失败关闭加载效果
    loading.value = false;
    // 登录失败的提示信息
    ElNotification({
      type: "error",
      message: (error as Error).message,
    });
  }
};
// 定义表单校验需要的配置对象
const rules = {
  username: [
    {
      required: true,
      min: 5,
      max: 10,
      message: "账号长度为5-10位",
      trigger: "change",
    },
  ],
  password: [
    {
      required: true,
      min: 6,
      max: 15,
      message: "密码长度为6-15位",
      trigger: "change",
    },
  ],
};
</script>

<style scoped lang="scss">
.login-container {
  width: 100%;
  height: 100vh;
  background: url("@/assets/images/background.jpg");
  background-repeat: no-repeat;
  background-size: cover;

  .el-row {
    margin: 0 !important;
  }

  .login_form {
    position: relative;
    width: 70%;
    top: 30vh;
    background: url("@/assets/images/background.jpg");
    padding: 40px;
    border-radius: 10px;
    box-shadow:
      10px 10px 30px rgba($color: #000000, $alpha: 0.5),
      -10px -10px 30px rgba($color: #ffffff, $alpha: 0.5),
      inset 5px 5px 10px rgba($color: #ffffff, $alpha: 0.5),
      inset -1px -1px 10px rgba($color: #000000, $alpha: 0.5);

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      color: white;
      font-size: 20px;
      margin: 20px 0;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
